int photoRPin = 0; 
int minLight;          //Used to calibrate the readings
int maxLight;          //Used to calibrate the readings
int lightLevel;
int adjustedLightLevel;
int count = 0;
boolean light_is_on = false; 

void setup() {
  Serial.begin(9600);
  lightLevel=analogRead(photoRPin);
  minLight=lightLevel-20;
  maxLight=lightLevel;
}

void loop(){
  //auto-adjust the minimum and maximum limits in real time
  lightLevel=analogRead(photoRPin);
  if(minLight>lightLevel) minLight=lightLevel;
  if(maxLight<lightLevel) maxLight=lightLevel; 
  adjustedLightLevel = map(lightLevel, minLight, maxLight, 0, 100); 
  if(!light_is_on && adjustedLightLevel > 50){
    count++;
    light_is_on = true;
  }
  else if(light_is_on && adjustedLightLevel <= 50) light_is_on = false;
  Serial.print("\nLight Level:");
  Serial.print(adjustedLightLevel);
  Serial.print(" - COUNT:");
  Serial.print(count);
  delay(30);
}

